home *** CD-ROM | disk | FTP | other *** search
- /* er_echo.c george jefferson george@mech.seas.upenn.edu 4/12/92 */
- /* replacement for the unix standard echo which writes to standard error */
- /* rather than standard out. */
- /* i wrote this 'cause brain-dead csh has no way to redirect to stderr, sigh */
- /* if there is a better way ( besides using sh! ) i'd sure like to know.. */
- /* */
- /* accepts a -n flag to suppress a newline, just like the real echo */
- /* */
- /* creeping featuritis... the following flags are also recognised */
- /* */
- /* -cat -> read from standard input, write to standard output */
- /* */
- /* '-level n` sets the output level to n ( default 0 ) */
- /* '-off n` sets the debug level to n ( default 0 ) */
- /* output is supressed whenever the 'off' level is lower than t output level */
- /* examples might help.... */
- /* er_echo -off 1 my dog has -level 2 ticks and -level 1 fleas */
- /* my dog has fleas */
- /* er_echo -off 2 my dog has -level 2 ticks and -level 1 fleas */
- /* my dog has ticks and fleas */
- /* if combined with -cat, the _last_ occurance of each of these flags is used*/
- /* */
- /* -log outputfile -> output is _appended_ to outputfile */
- /* rather than going to stderr */
- /* multiple -log arguments are processed until a valid one is found */
- /* eg er_echo "-log /etc/passwd -log a -log b hi" */
- /* writes to "a" ( unless u r root! ) */
- #include <stdio.h>
- main (argc,argv)
- int argc; char **argv;
- {
- int i,newline=1,iout=0,cat=0;
- FILE *logfile;
- char *byte[1],debug[3],off[3];
- logfile=stderr;
- for( i = 1 ; i < argc ; i++ )
- if( strcmp(argv[i],"-log") == 0 &&
- ( logfile=fopen(argv[++i],"a") ) != NULL )break;
- if( ( logfile ) == NULL )logfile=fopen("/dev/null","a");
- /* we were trying to redirect, I guess */
- /* dont want to see this message */
- strcpy(off,"0");strcpy(debug,"0");
- for( i = 1 ; i < argc ; i++ )
- {
- if( strcmp(argv[i],"-cat") == 0 )cat=1;
- else if(strcmp(argv[i],"-level") == 0 )strcpy(debug,argv[++i]);
- else if(strcmp(argv[i],"-off") == 0 )strcpy(off,argv[++i]);
- }
- if( cat == 1 && strcmp(off,debug) >= 0 ) /*write input to output */
- {
- while(i=read(0,byte,1) != 0 )
- {
- fprintf(logfile,"%s",byte);
- }
- exit(0);
- }
- else if( cat == 1) /* just dump input */
- {
- while(i=read(0,byte,1) != 0 );
- exit(0);
- }
- else /* write command line arguments to output */
- {
- strcpy(off,"0");strcpy(debug,"0");
- for( i = 1 ; i < argc ; i++ )
- {
- if(strcmp(argv[i],"-n") == 0 )newline=0;
- else if(strcmp(argv[i],"-off") == 0 )strcpy(off,argv[++i]);
- else if(strcmp(argv[i],"-level") == 0 )strcpy(debug,argv[++i]);
- else if(strcmp(argv[i],"-cat") == 0 );
- else if(strcmp(argv[i],"-log") == 0 )i++;
- else if(strcmp(off,debug) >= 0 )
- {
- if(iout++!=0)fprintf(logfile," ");
- fprintf(logfile,"%s",argv[i]);
- }
- }
- if(newline != 0 && iout != 0 )fprintf(logfile,"\n");
- exit(0);
- }
- }
-
-
-
-